sugarfreeApp.controller('MainController', ['$rootScope','$scope', '$http', '$location','$routeParams','$cookies', '$window', '$modal', 'Utils', 'AUTH_EVENTS', 'WebsocketService','TimeService',
		function ($rootScope,$scope, $http, $location,$routeParams,$cookies, $window, $modal, Utils, AUTH_EVENTS, WebsocketService,timeService) {
  "use strict";
  $scope.utils = Utils;

  $scope.isRoute = function (path) {
   	// 	console.log('aaa', path, $location.absUrl())
	//  console.info("#$location.path()#",$location.path());
	//  console.info('$routeParams',$routeParams);
    return $location.path() === path;
  };

  $scope.focusBox = function(){
	  Utils.focusBox();
  };
  
  $scope.focusTopBox = function(){
	  Utils.focusTopBox();
  };
  
  $scope.setParclose = function(isSet){
	  Utils.setParclose(isSet);
  };
  
  $scope.setTopParclose = function(isSet){
	  Utils.setTopParclose(isSet);
  };
  
  $scope.setProcessParclose = function(isSet){
	  isSet = isSet || false; 
	  Utils.setProcessParclose(isSet);
  };
  
  $scope.logout = function () {
  	if(confirm('确定要退出平台吗？')){
  	  	$http.get("logout").success(function (data) {
      		$window.location.href = "index.html";
    	});
  	}else{
  		console.log("Un logout.");
  	}
    
  };

  $scope.openQR = function () {

    var modalSize = 'sm';

    var modalInstance = $modal.open({
      animation: true,
      templateUrl: 'views/qrmodal.html',
      controller: 'QRModalCtrl',
      size: modalSize,
      resolve: {
        qrString: function() {
          return $scope.qrString;
        },
        qrTitle: function(){
        	return $scope.currentUser.name;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      console.log('Modal item selected', selectedItem);
    }, function () {
      console.log('Modal dismissed');
    });
  };

  
  $scope.openLogin = function () {
    var modalSize = '';

    var modalInstance = $modal.open({
      animation: true,
      templateUrl: 'partials/login-form.html',
      controller: 'QRModalCtrl',
      size: modalSize,
      resolve: {
        qrString: function () {
          return $scope.qrString;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      console.log('Login Modal item selected', selectedItem);
    }, function () {
      console.log("Login Modal dismissed", $location.absUrl());
      if ($location.absUrl().indexOf("page_index.html") > -1) {
        // redirect to main page
        console.log("to be redirect to login page");
        $window.location.href = "index.html";
      }
    });
  };
  
  
  $scope.openChangePWD = function(){
  	var modalSize = 'sm';
  	var modalInstance = $modal.open({
      animation: true,
      templateUrl: 'views/change-pwd.html',
      controller: 'ChangePWDCtrl',
      size: modalSize,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });
  }
  
  //
  $scope.showDetail = function(){
	  $window.location.href = "#/profile";
  };

  //$scope.uid = $cookies.get('UID');
  //$scope.name = $cookies.get('NAME');
  //$scope.logo = $cookies.get('LOGO');
  

  $scope.$on(AUTH_EVENTS.notAuthenticated, function (event, data) {
    console.error("401未授权");
    $window.location.href = "index.html";
  });
  
  
  $scope.toggleBaseLessonMenu=function(){
//	console.info('toggleBaseLessonMenu',$location.url());
	var actionUrl = '/grouplessons/0/2';
	if($scope.showBaseLessonMenu){
		if($location.url().indexOf('grouplessons')==-1){
			$location.url(actionUrl);
		}else{
			$scope.showBaseLessonMenu = !$scope.showBaseLessonMenu;
		}
	}else{
		$scope.showBaseLessonMenu = true;
		$location.url(actionUrl);
	}
  };
  
  $scope.togglePrivateLessonMenu=function(){
  	var actionUrl = '/privatelessons/0/3';
	if($scope.showPrivateLessonMenu){
		if($location.url().indexOf('privatelessons')==-1){
			$location.url(actionUrl);
		}else{
			$scope.showPrivateLessonMenu = !$scope.showPrivateLessonMenu;
		}
	}else{
		$scope.showPrivateLessonMenu = true;
		$location.url(actionUrl);
	}
  };
  
  
  //
  function initData(){
  $scope.qrString = "";	
  	  $http.get("profile/get")
  	  		.then(function(rsps){
  			  //console.info("data",rsps);
  			  var gym = rsps.data;
			  $scope.currentUser = {
			  	uid:gym.id,
			  	name:gym.name,
			  	logo:gym.logo
			  };
  	  		  $scope.qrString = "sugarfree,1," + gym.id;		
  	  		},function(reps){
  	  			console.error("登录出错-main",reps);
  	  			alert("登录出错！");
  	  			$window.location.href = "index.html";
  	  		});
  	  		
	  //console.info("rootScope",$rootScope);
	  $scope.receipts = WebsocketService.receipts;
	  //$cookies.remove('UID');
      //$cookies.remove('NAME');
      //$cookies.remove('LOGO');
      
      $scope.weekRange={
      	"week0":timeService.getDateByWeekAndDayOfWeek(0,1).substr(5) + 
      			"~" + timeService.getDateByWeekAndDayOfWeek(0,7).substr(5),
      	"week1":timeService.getDateByWeekAndDayOfWeek(1,1).substr(5) + 
      			"~" + timeService.getDateByWeekAndDayOfWeek(1,7).substr(5),
      	"week2":timeService.getDateByWeekAndDayOfWeek(2,1).substr(5) + 
      			"~" + timeService.getDateByWeekAndDayOfWeek(2,7).substr(5),
      };
  }
  initData();
}]);
